package com.manor.main.fragment.user

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.manor.common.Constants
import com.manor.common.bean.ShortVideoBean
import com.manor.common.fragment.BaseFragment
import com.manor.common.http.HttpCallback
import com.manor.common.ktx.toBeanList
import com.manor.main.R
import com.manor.main.activity.UserVideoDetailActivity
import com.manor.main.adapter.UserVideoListAdapter
import kotlinx.android.synthetic.main.fragment_user_video.*

/**
 * 个人资料--我的视频
 *
 *
 * @date  2020/7/14  18:05
 * @version 1.0
 */
class UserVideoFragment : BaseFragment() {
    private val startPage = 1
    private var pageNum: Int = startPage
    private var mUid = ""

    private val dataList: MutableList<ShortVideoBean> = mutableListOf()
    private lateinit var mAdapter: UserVideoListAdapter

    companion object {
        fun newInstance(uId: String): UserVideoFragment {
            return UserVideoFragment().apply {
                arguments = Bundle().apply {
                    putString(Constants.TO_UID, uId)
                }
            }
        }
    }


    override fun getLayoutRes(): Int = R.layout.fragment_user_video

    override fun initView() {
        mUid = arguments?.getString(Constants.TO_UID) ?: ""

        mRecycler.layoutManager = GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false)
        mAdapter = UserVideoListAdapter(R.layout.item_user_list_video, dataList)
        mRecycler.adapter = mAdapter
        mAdapter.setOnItemClickListener(itemClickListener)
        smartRefreshLayout.setOnRefreshListener(onRefreshListener)
        smartRefreshLayout.setOnLoadMoreListener(onLoadMoreListener)

    }

    override fun onResume() {
        super.onResume()
        if (UserSvManager.dataSource.size > 0 && UserSvManager.dataSource.size > UserSvManager.syncPos) {
            pageNum = UserSvManager.curPageNum
            if (dataList.size != UserSvManager.dataSource.size) {
                dataList.clear()
                dataList.addAll(UserSvManager.dataSource)
                mAdapter.notifyItemRangeChanged(0, dataList.size)
                mRecycler.scrollToPosition(UserSvManager.syncPos)
            } else {
                mRecycler.scrollToPosition(UserSvManager.syncPos)
            }

        }

    }




    fun onRefresh(dataSource: MutableList<ShortVideoBean>) {
        val oldSize = dataList.size
        dataList.clear()
        mAdapter.notifyItemRangeRemoved(0, oldSize)
        dataList.addAll(dataSource)
        mAdapter.notifyItemRangeChanged(0, dataSource.size)
        if (null != smartRefreshLayout) {
            smartRefreshLayout.finishRefresh()
            smartRefreshLayout.autoLoadMore()
        }

        // mRecycler.scrollToPosition(0)

    }


    private val onRefreshListener = OnRefreshListener {
        pageNum = startPage
        smartRefreshLayout.finishRefresh(2 * 1000)
    }

    private val onLoadMoreListener = OnLoadMoreListener {
        pageNum++
        smartRefreshLayout.finishLoadMore(1500)
    }

    private val itemClickListener = OnItemClickListener { adapter, view, pos ->
        UserSvManager.record(pos)
        UserSvManager.savePage(pageNum)
        UserSvManager.addDataSource(dataList)
        context?.let { UserVideoDetailActivity.forward(it,mUid,"","") }
    }


}